﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：CouponsManage.cs
  //文件功能描述：优惠劵管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class CouponsManage
    {
        private readonly DALCoupons dalCoupon = new DALCoupons();
        private readonly DALUserCoupons dalUserCoupon = new DALUserCoupons();

        #region "分页查询优惠劵信息 by 赵国伟 2012-04-14"
        /// <summary>
        /// 分页查询优惠劵信息
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="useStatus"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<CouponInfo> QueryListAsPage(QueryCouponsAsPage q)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(q.KeyWords))
                cond += " and CouponID like '%" + q.KeyWords + "%' ";

            if (!string.IsNullOrEmpty(q.UseStatus))
                cond += " and UseStatus='" + q.UseStatus + "' ";
            if (!string.IsNullOrEmpty(q.GenerateMethod))
                cond += " and GenerateMethod='" + q.GenerateMethod + "' ";
            if (q.AllotStatus == AllotStatusEnum.未分配.ToString())
                cond += " and CouponID not in(Select CouponID From UserCoupons) ";
            else if(q.AllotStatus == AllotStatusEnum.已分配.ToString())
                cond += " and CouponID in(Select CouponID From UserCoupons) ";

            if (q.ExpiredStatus == ExpiredStatusEnum.有效.ToString())
                cond += " and Datediff(d,getdate(),EndDate)>=0 ";
            else if(q.ExpiredStatus == ExpiredStatusEnum.已过期.ToString())
                cond += " and Datediff(d,getdate(),EndDate)<0 ";

            string orderBy = "CouponID desc";

            List<Coupons> listOfSource = dalCoupon.QueryListAsPage(cond, orderBy, q.CurPage,q.PageSize, out q.TotalCount);
            List<CouponInfo> listOfTarget = null;
            if (listOfSource != null)
            {
                listOfTarget = new List<CouponInfo>();
                Dictionary<int, IDNameObj> dictOwner = dalUserCoupon.QueryOwnersToHash("CouponID in (Select couponID from Coupons Where " + cond + ")");
                foreach (Coupons s in listOfSource)
                {
                    CouponInfo t = new CouponInfo();
                    t.Coupon = s;
                    if (dictOwner.ContainsKey(s.CouponID))
                        t.Owner = dictOwner[s.CouponID];
                    listOfTarget.Add(t);
                }
            }
            return listOfTarget;
        }
        #endregion

        #region "查询一条优惠劵信息； by 赵国伟 2012-04-14"
        /// <summary>
        /// 查询一条优惠劵信息；
        /// </summary>
        /// <returns></returns>
        public Coupons GetCouponInfo(int CouponID)
        {
            return dalCoupon.GetModel(CouponID);
        }
        public CouponInfo GetCoupon(int CouponID)
        {
            Coupons s = dalCoupon.GetModel(CouponID);
            CouponInfo t = null;
            if (s != null)
            {
                t = new CouponInfo();
                t.Coupon = s;
                Dictionary<int, IDNameObj> dictOwner = dalUserCoupon.QueryOwnersToHash("CouponID = " + CouponID);
                if (dictOwner.ContainsKey(s.CouponID))
                    t.Owner = dictOwner[s.CouponID]; 
            }
            return t;
        }
        #endregion


        #region "生成指定数量的优惠劵 by 赵国伟 2012-04-15"
        /// <summary>
        /// 生成指定数量的优惠劵
        /// </summary>
        /// <param name="c"></param>
        /// <param name="Quantity"></param>
        /// <returns></returns>
        public ReturnValue Generate(Coupons c, int Quantity)
        {
            c.CreateDate = DateTime.Now;
            return dalCoupon.Generate(c, Quantity);
        }
        #endregion

        #region "更新指定的优惠劵信息； by 赵国伟 2012-4-15"
        /// <summary>
        /// 更新指定的优惠劵信息；
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public ReturnValue Update(Coupons c)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"更新失败！");
            Coupons o = dalCoupon.GetModel(c.CouponID);
            c.CreateDate = o.CreateDate;
            c.GenerateMethod = o.GenerateMethod;
            if (dalCoupon.Update(c) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "保存成功！";
            }
            return ret;
        }
        #endregion

        #region "删除指定的优惠劵 by 赵国伟 2012-04-15"
        /// <summary>
        /// 删除指定的优惠劵
        /// </summary>
        /// <param name="CouponID"></param>
        /// <returns></returns>
        public ReturnValue DeleteCoupon(int CouponID)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail,"删除失败！");
            try
            {
                if (dalCoupon.Delete(CouponID) > 0)
                {
                    ret.Message = "删除成功！";
                    ret.Result = ResultType.Success;
                }
            }
            catch (Exception e)
            {
                ret.Message = "删除失败 可能的原因(该优惠劵已分配或被使用！)";
            }
            return ret;
        }
        #endregion

        #region "分配指定的优惠劵 by 赵国伟 2012-04-21"
        /// <summary>
        /// 分配指定的优惠劵
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public ReturnValue SaveAssign(UserCoupons c)
        {
            c.GainDate = DateTime.Now;
            c.GainMethod = GainMethodEnum.商城赠送.ToString();
            return dalUserCoupon.SaveAssign(c);
        }
        #endregion

        #region "查询指定条件下的优惠劵信息 by 赵国伟 2012-04-24"
        /// <summary>
        /// 查询指定条件下的优惠劵信息
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public Dictionary<int, Coupons> QueryListToHash(string cond)
        {
            Dictionary<int, Coupons> dict = new Dictionary<int, Coupons>();
            List<Coupons> listOfData = dalCoupon.QueryList(cond, "");
            if (listOfData != null)
            {
                foreach (Coupons d in listOfData)
                {
                    dict.Add(d.CouponID, d);
                }
            }
            return dict;
        }
        #endregion


        #region "获取指定用户可用的优惠券 by 赵国伟 2012-08-20"
        /// <summary>
        /// 获取指定用户可用的优惠券
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Coupons> GetActiveCouponsByUserId(int userId)
        {
            string cond = " UseStatus='"+ CouponUseStatus.未用.ToString() +"' and EndDate >='" + DateTime.Now.ToString("yyyy-MM-dd") + "' ";
            cond += "  and Couponid in (Select Couponid from UserCoupons Where UserID=" + userId + ")";
            return dalCoupon.QueryList(cond, "EndDate,CouponID");
        }
        #endregion


    }
}
